import 'package:flutter/material.dart';
import 'package:audioplayers/audioplayers.dart';

class Example_03 extends StatefulWidget {
  const Example_03({super.key});

  @override
  State<Example_03> createState() => _Example_03State();
}

class _Example_03State extends State<Example_03> {
  // 解析颜色参数
  Color parseColorCode(String colorCode) {
    if (colorCode == 'red') {
      return Colors.red;
    } else if (colorCode == 'orange') {
      return Colors.orange;
    } else if (colorCode == 'yellow') {
      return Colors.yellow;
    } else if (colorCode == 'green') {
      return Colors.green;
    }else if (colorCode == 'blue') {
      return Colors.blue;
    } else if (colorCode == 'purple') {
      return Colors.purple;
    }else {
      return Colors.pink; // 默认颜色，防止报错
    }
  }
  // 创建widget
  Widget createItem(int audioNumber, String colorCode) {
    return Row(
      children: [
        Expanded(
          child: ElevatedButton(
              style: ElevatedButton.styleFrom(
                backgroundColor: parseColorCode(colorCode),
                shape: RoundedRectangleBorder(borderRadius: BorderRadius.zero),
              ),
              child: Text('$audioNumber声调'),
              onPressed: () {
                AudioCache.instance = AudioCache(prefix: '');
                final player = AudioPlayer();
                player.play(AssetSource('audios/note$audioNumber.wav'));
              }
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      appBar: AppBar(
        title: Text('木琴小音乐'),
        backgroundColor: Colors.purple,
      ),
      body: Center(
          child: Column(
            children: [
              createItem(1, 'red'),
              createItem(2, 'orange'),
              createItem(3, 'yellow'),
              createItem(4, 'green'),
              createItem(5, 'teal'),
              createItem(6, 'blue'),
              createItem(7, 'purple')
            ],
          )
      )
    );
  }
}
